<?php 
include 'method/win.php';

if(isset($_GET['page'])){
    $page = $_GET['page'];
}else {
    $page = 1;
}

if(isset($_GET['id'])){
//     微博
//     $_SESSION['weiboid'] = $_GET['id'];
    $wbsql = "SELECT weibo_user.*, weibo_list.* FROM weibo_user, weibo_list WHERE weibo_list.weibo_id=".$_GET['id']." AND weibo_user.user_name = weibo_list.author";
    $wbres = $conn->query($wbsql);
    $wbrow = $wbres->fetch_assoc();
    $cmsql = "SELECT weibo_user.user_tx, weibo_comment.* FROM weibo_comment, weibo_user WHERE weibo_comment.weibo_id=".$wbrow['weibo_id']." AND weibo_user.user_name = weibo_comment.cm_author";
    $cmres = $conn->query($cmsql);
//     评论
    $cmper_page = 10;
    if(!isset($_GET['cmpage'])){
        $cmpage = 1;
    }else{
        $cmpage = $_GET['cmpage'];
    }
    
    $cmtotal_ly = $cmres->num_rows;
    $cmtotal_page = ceil($cmtotal_ly/$cmper_page);
    
    $cmsql = "SELECT weibo_user.user_tx, weibo_comment.* FROM weibo_comment, weibo_user WHERE weibo_comment.weibo_id=".$wbrow['weibo_id']." AND weibo_user.user_name = weibo_comment.cm_author limit ".($cmpage-1)*$cmper_page.",".$cmper_page;
    $cmres = $conn->query($cmsql);
//     转发
    $relsql = "SELECT weibo_list.*, weibo_user.user_tx FROM weibo_list, weibo_user WHERE relwb_id = ".$wbrow['weibo_id']." AND weibo_list.author = weibo_user.user_name";
    $relres = $conn->query($relsql);
}else {
//     微博
    $wbsql = "SELECT weibo_user.user_tx, weibo_list.* FROM weibo_user, weibo_list WHERE weibo_user.user_name = weibo_list.author limit 1";
    $wbres = $conn->query($wbsql);
    $wbrow = $wbres->fetch_assoc();
//     评论
    $cmsql = "SELECT weibo_user.user_tx, weibo_comment.* FROM weibo_comment, weibo_user WHERE weibo_comment.weibo_id=".$wbrow['weibo_id']." AND weibo_user.user_name = weibo_comment.cm_author";
    $cmres = $conn->query($cmsql);
    
    $cmper_page = 10;
    if(!isset($_GET['cmpage'])){
        $cmpage = 1;
    }else{
        $cmpage = $_GET['cmpage'];
    }
    
    $cmtotal_ly = $cmres->num_rows;
    $cmtotal_page = ceil($cmtotal_ly/$cmper_page);
    
    $cmsql = "SELECT weibo_user.user_tx, weibo_comment.* FROM weibo_comment, weibo_user WHERE weibo_comment.weibo_id=".$wbrow['weibo_id']." AND weibo_user.user_name = weibo_comment.cm_author limit ".($cmpage-1)*$cmper_page.",".$cmper_page;
    $cmres = $conn->query($cmsql);
    //     转发
    $relsql = "SELECT weibo_list.*, weibo_user.user_tx FROM weibo_list, weibo_user WHERE relwb_id = ".$wbrow['weibo_id']." AND weibo_list.author = weibo_user.user_name";
    $relres = $conn->query($relsql);
}
if(isset($_SESSION['user'])){
    header("location:detail_loginafter.php?id=".$_GET['id']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>未登录微博详情页面</title>
    <link rel="stylesheet" href="../lib/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/detail.css">
    <script src="../lib/jquery/jquery.js" type="text/javascript"></script>
    <script src="../lib/bootstrap/js/bootstrap.js" type="text/javascript"></script>
</head>
<body>
<!--顶部导航条-->
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!--头部logo-->
        <div class="navbar-header">
            <a class="navbar-brand" href="#"><em><img src="../img/logo.jpg" height=30 width=30></em><em>小狗微博</em></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!--搜索-->
            <form class="navbar-form navbar-left" role="search" method="post" action="search.php">
                <div class="form-group">
                    <input type="text" class="form-control" style="width: 400px" placeholder="Search" name="str-search">
                </div>
                <button type="sumbit" class="btn btn-default" name="search-btn">搜索</button>
            </form>
            <!--首页、注册登录-->
            <ul class="nav navbar-nav navbar-right">
                <li><a href="index.php">首页</a></li>
                <li><a href="register.php">注册</a></li>
                <li><a href="login.php">登录</a></li>

            </ul>
        </div>
    </div>
</nav>

<!--详情版心-->
<div class="detail-warp  clearfix">
    <div class="left-warp">
        <!--微博板块-->
        <div class="weibo-box clearfix">
            <!--作者头像-->
            <div class="author-tx">
                <img src="../<?php echo $wbrow['user_tx']?>" style="width: 50px; height: 50px; border-radius: 50%">
            </div>
            <!--内容版心-->
            <div class="content-warp">
                <!--用户名-->
                <div class="author-name">
                    <a href="person.php?id=<?php echo $wbrow['user_id']?>"><?php echo $wbrow['author']?></a>
                </div>
                <!--发博时间-->
                <div class="weibo-time">
                    <?php echo $wbrow['public_time']?>
                </div>
                <!--微博内容-->
                <div class="weibo-content">
                    <?php echo $wbrow['weibo_content']?>
                </div>
            </div>
        </div>
        
        <?php 
        if($wbrow['relwb_id']==""){
            //         不是转发的
            echo "<div class='weibo-photo'>";
            if($wbrow['weibo_img'] != ''){
                echo "<img src='../".$wbrow['weibo_img']."' style='width: 500px; height: auto'>";
            }
            echo "</div>";
        }else {
            $relsql = "SELECT * FROM weibo_list WHERE weibo_id=".$wbrow['relwb_id']."";
            $relres = $conn->query($relsql);
            $relrow = $relres->fetch_assoc();
            
            //         转发的
            echo "<div class='rel-box'>";
            //         源用户名
            echo "<div class='author-name'><a href='#'>".$relrow['author']."</a></div>";
            //         源微博内容
            echo "<a href='detail.php?id=".$relrow['weibo_id']."'><div class='weibo-content'>
                ".$relrow['weibo_content']."
             </div>";
            //         源图片
            if($relrow['weibo_img'] != ''){
                echo "<div class='rel-photo'><img src='../".$relrow['weibo_img']."' style='width: auto; height: 167px'></div>";
            }
            //         源微博时间
            echo "<div class='rel-time'>".$relrow['public_time']."</div>";
            
            $rrsql = "select * from weibo_list where relwb_id = ".$relrow['weibo_id'];
            $rrres = $conn->query($rrsql);
            
            $rcsql = "select * from weibo_comment where weibo_id = ".$relrow['weibo_id'];
            $rcres = $conn->query($rcsql);
            $rkcsql = "select * from weibo_kidcomment where weibo_id = ".$relrow['weibo_id'];
            $rkcres = $conn->query($rkcsql);
            $rcnum = ($rcres->num_rows)+($rkcres->num_rows);
            //         源微博操作
            echo "<div class='rel-op'><span><em class='glyphicon glyphicon-share'></em><em>".$rrres->num_rows."</em></span>";
            echo "<span>|</span><span><em class='glyphicon glyphicon-comment'></em><em>".$rcnum."</em></span>";
            echo "<span>|</span><span><em class='glyphicon glyphicon-thumbs-up'></em><em>".$relrow['weibo_zan']."</em></span></div></a></div>";
            
        }
        
        $rsql = "select * from weibo_list where relwb_id = ".$wbrow['weibo_id'];
        $rres = $conn->query($rsql);
        
        $csql = "select * from weibo_comment where weibo_id = ".$wbrow['weibo_id'];
        $cres = $conn->query($csql);
        $kcsql = "select * from weibo_kidcomment where weibo_id = ".$wbrow['weibo_id'];
        $kcres = $conn->query($kcsql);
        $cnum = ($cres->num_rows)+($kcres->num_rows);
        ?>

        <!--微博操作-->
        <div class="weibo-op clearfix">
            <a href="javascript::" onclick="tipLogin()"><span><em class="glyphicon glyphicon-star-empty"></em><em>收藏</em></span></a>
            <a href="detail.php?id=<?php echo $wbrow['weibo_id']?>&page=2" class="rel-btn"><span><em class="glyphicon glyphicon-share"></em><em><?php echo $rres->num_rows?></em></span></a>
            <a href="detail.php?id=<?php echo $wbrow['weibo_id']?>&page=1" class="cm-btn" style="color: black"><span><em class="glyphicon glyphicon-comment"></em><em><?php echo $cnum?></em></span></a>
            <a href="javascript::" onclick="tipLogin()"><span><em class="glyphicon glyphicon-thumbs-up"></em><em><?php echo $wbrow['weibo_zan']?></em></span></a>
        </div>

		<div class="comment choose-2">
    		<!--评论板块-->
            <div class="comment-warp clearfix">
                <!--评论者头像-->
                <div class="cm-tx">
                    <img src="../img/person.jpg" style="width: 30px; height: 30px">
                </div>
                
                    <!--评论者操作-->
                    <div class="cm-op clearfix">
                        <textarea rows="1"></textarea>
                        <button onclick="tipLogin()">评论</button>
                    </div>
                
            </div>
			<!--评论列表-->
            <div class="cm-list">
            	<?php 
                    if($cmres->num_rows){
                        while ($row = $cmres->fetch_assoc()){
    //                         评论盒子
                            echo "<div class='comment-box clearfix'>";
                            echo "<div class='cm-tx'><img src='../".$row['user_tx']."' style='width: 30px; height: 30px'></div>";
    //                         评论内容版心
                            echo "<div class='cmct-warp'>";
    //                         评论内容
                            echo "<div class='cm-content'>".$row['cm_author'].":".$row['cm_content']."</div>";
    //                         评论日期
                            echo "<div class='cm-time clearfix'>".$row['cm_time'];
                            echo"<span style='float: right'><a href='javascript:' onclick='tipLogin()'>回复</a></span></div>";
                            
                            $kcsql = "SELECT weibo_kidcomment.* FROM weibo_comment, weibo_kidcomment WHERE weibo_kidcomment.cm_id=weibo_comment.cm_id AND weibo_comment.cm_id=".$row['cm_id']."";
                            $kcres = $conn->query($kcsql);
                            if($kcres->num_rows){
                                //                         子评论表
                                echo "<a href='javascript::' onclick='tipLogin()'><div class='cmkid-list'>共".$kcres->num_rows."条评论</div></a>";
                            }
                            echo "</div></div>";
                            //         加载更多
                            if($cmpage!="1"){
                                if($cmpage==1){
                                    echo "<div class='loadmore clearfix'>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>首页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>上一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage+1)."'>";
                                    echo "<span>下一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>尾页</span>";
                                    echo "</a>";
                                    echo "</div>";
                                }else if($cmpage==$cmtotal_page){
                                    echo "<div class='loadmore clearfix'>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>首页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage-1)."'>";
                                    echo "<span>上一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>下一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>尾页</span>";
                                    echo "</a>";
                                    echo "</div>";
                                }else {
                                    echo "<div class='loadmore clearfix'>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=1'>";
                                    echo "<span>首页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage-1)."'>";
                                    echo "<span>上一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".($cmpage+1)."'>";
                                    echo "<span>下一页</span>";
                                    echo "</a>";
                                    echo "<a href='detail.php?id=".$wbrow['weibo_id']."&cmpage=".$cmtotal_page."'>";
                                    echo "<span>尾页</span>";
                                    echo "</a>";
                                    echo "</div>";
                                }
                            }
                        }  
                    }
                ?>
            </div>
		</div>
        
        
        
        <div class="rel">
            <!--转发板块-->
            <div class="comment-warp clearfix">
                
                    <!--转发者操作-->
                    <div class="zf-op clearfix">
                        <textarea rows="3"></textarea>
                        <button onclick="tipLogin()">转发</button>
                    </div>
                
            </div>

            <!--转发附带内容列表-->
            <div class="cm-list">
            <?php 
            if($relres->num_rows){
                while ($row = $relres->fetch_assoc()){
                    //             评论盒子
                    echo "<div class='comment-box clearfix'>";
                    echo "<div class='cm-tx'><img src='../".$row['user_tx']."' style='width: 30px; height: 30px'></div>";
                    //             评论内容版心
                    echo "<div class='cmct-warp'>";
                    //             评论内容
                    echo "<div class='cm-content'>".$row['author'].":".$row['weibo_content']."</div>";
                    //             评论日期
                    echo "<div class='cm-time'>".$row['public_time']."</div></div></div>";
                }
            }else {
                echo "暂无转发";
            }

            ?>
            </div>
        </div>
    </div>
    <div class="right-warp">
        <div class="logo">
            <img src="../img/logo1.jpg" style="width: 260px; height: 260px">
        </div>
    </div>
</div>
</body>
<!-- 脚本 -->
<script>
    function tipLogin(){
        confirm("请先登录")
    }

    if(<?php echo $page?> == "1"){
    	$(".rel-btn").css("color","#888")
        $(".cm-btn").css("color","black")
        $(".comment").removeClass("choose-2")
        $(".rel").removeClass("choose-2")
        $(".comment").addClass("choose-2")
    }else{
    	$(".cm-btn").css("color","#888")
        $(".rel-btn").css("color","black")
        $(".comment").removeClass("choose-2")
        $(".rel").removeClass("choose-2")
        $(".rel").addClass("choose-2")
    }
</script>
</html>